package com.chuantou.yuba.ui.widget;

import android.content.Context;
import android.graphics.PointF;
import android.support.v4.view.ViewConfigurationCompat;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;

public class ChildViewPager extends ViewPager {

    /** 触摸时按下的点 **/
    PointF                mDownPoint = new PointF();
    /** 上次触摸的点 **/
    PointF                mLastPoint  = new PointF();
    /**点击敏感度*/
    private int mTouchSlop;
    /** 是否已经拖动过 */
    private boolean mHasDragged = false;
    OnSingleTouchListener onSingleTouchListener;

    public ChildViewPager(Context context) {
        super( context);
        final ViewConfiguration configuration = ViewConfiguration.get( getContext());
        mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop( configuration);
    }
    
    public ChildViewPager(Context context, AttributeSet attrs) {
        super( context, attrs);
        final ViewConfiguration configuration = ViewConfiguration.get( getContext());
        mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop( configuration);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent arg0) {
        // 当拦截触摸事件到达此位置的时候，返回true，
        // 说明将onTouch拦截在此控件，进而执行此控件的onTouchEvent
        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        // 每次进行onTouch事件都记录当前的按下的坐标
        if(ev.getAction() == MotionEvent.ACTION_DOWN) {
            // 记录按下时候的坐标
            // 切记不可用 downP = curP ，这样在改变curP的时候，downP也会改变
            mDownPoint.x = ev.getX();
            mDownPoint.y = ev.getY();
            mLastPoint.x = ev.getX();
            mLastPoint.y = ev.getY();
            mHasDragged = false;
            // 此句代码是为了通知他的父ViewPager现在进行的是本控件的操作，不要对我的操作进行干扰
            getParent().requestDisallowInterceptTouchEvent( true);
        }

        if(ev.getAction() == MotionEvent.ACTION_MOVE) {
            // 此句代码是为了通知他的父ViewPager现在进行的是本控件的操作，不要对我的操作进行干扰
            float currentX = ev.getX();
            float currentY = ev.getY();
            float disLastX = currentX - mLastPoint.x;
            float disLastY = currentY - mLastPoint.y;
            float disFirstX = currentX - mDownPoint.x;
            float disFirstY = currentY - mDownPoint.y;
            // 横向滑动，让父类不拦截事件
            if(Math.abs( disLastX) >= Math.abs( disLastY)) {
                getParent().requestDisallowInterceptTouchEvent( true);
            }else{
                getParent().requestDisallowInterceptTouchEvent( false);
            }
            // 只要有过滑动迹象，就认为不是点击事件
            if(Math.abs( disLastX) > mTouchSlop || Math.abs( disLastY) > mTouchSlop || Math.abs( disFirstX) > mTouchSlop
                || Math.abs( disFirstY) > mTouchSlop) {
                mHasDragged = true;
            }
            mLastPoint.x = currentX;
            mLastPoint.y = currentY;
        }

        if(ev.getAction() == MotionEvent.ACTION_UP) {
            //如果没有滑动迹象，则认为是点击事件，
            if(!mHasDragged){
                onSingleTouch();
                return true;
            }
        }

        return super.onTouchEvent( ev);
    }

    /**
     * 单击
     */
    public void onSingleTouch() {
        if(onSingleTouchListener != null) {
            onSingleTouchListener.onSingleTouch(getCurrentItem());
        }
    }

    /**
     * 创建点击事件接口
     */
    public interface OnSingleTouchListener {

        public void onSingleTouch(int position);
    }

    public void setOnSingleTouchListener(OnSingleTouchListener onSingleTouchListener) {
        this.onSingleTouchListener = onSingleTouchListener;
    }

}
